package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyKnowledge;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.entity.db.res.YxyResBookContentsKnoledges;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyKnowledgeRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsKnoledgesRepository;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ActivityContentKnowController extends YxyApiBaseController {

    @Autowired
    private YxyResBookContentsRepository yxyResBookContentsRepository;

    @Autowired
    private YxyResBookContentsKnoledgesRepository yxyResBookContentsKnoledgesRepository;

    @Autowired
    private YxyKnowledgeRepository yxyKnowledgeRepository;

    @ApiOperation(value = "获得知识点")
    @RequestMapping(value = "/api_yw/home/activity/tea/content/know", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {
        List<RespModel> list = new ArrayList<RespModel>();
        RespModel model = null;
        // 幼儿园 幼儿园没有知识点 知识点就是章本身 id 为 double 章id
        if ("00".equals(reqModel.phase)) {
            model = new RespModel();
            YxyResBookContents contents = yxyResBookContentsRepository.findById(reqModel.contentId);
            model.name = contents.getName();
            String id1 = contents.getId().substring(6, contents.getId().length());
            model.id = id1;
            list.add(model);
            // 其它
        } else {
            List<YxyResBookContentsKnoledges> contentIds = yxyResBookContentsKnoledgesRepository
                    .findByBookContentId(reqModel.contentId);
            List<YxyKnowledge> list2 = yxyKnowledgeRepository
                    .findByIdIn(contentIds.stream().map(s -> s.getKnowledgeId()).collect(Collectors.toList()));
            for (YxyKnowledge yxyKnowledge : list2) {
                model = new RespModel();
                model.id = yxyKnowledge.getId() + "";
                model.name = yxyKnowledge.getName();
                list.add(model);
            }
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String contentId;
        public String phase;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请填写学段");
            Assert.notNull(StringUtils.defaultIfBlank(contentId, null), "请填写目录");
        }

    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String name;

    }
}
